<template>
	<view>
		<view class="">
			<input type="text" v-model="input" style="border: 1px solid black;"  placeholder="请输入消息"/>
		<el-button @click="handleSendButton()">发送给user1</el-button>
		<view class="">
			收到的消息:{{getmessage.message}}
		</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				getmessage:'',
				input:'',
				inputform:{
					id:2,
					touxiang:'',
					message:'',
					type:0,
					time:'2024年4月22日 21:26'
				},
			}
		},
		onLoad() {
			this.webSocketInit()
		},
		methods: {
			webSocketInit(){
			      const webSocketUrl = 'ws://127.0.0.1:8800'
			      this.webSocketObject = new WebSocket(webSocketUrl);
			      this.webSocketObject.onopen = this.webSocketOnOpen
			      this.webSocketObject.onmessage = this.webSocketOnMessage
			      this.webSocketObject.onerror = this.webSocketOnError
			      this.webSocketObject.onclose = this.webSocketOnClose
			
			    },
			    webSocketOnOpen(e){
			      console.log('与服务端连接打开->',e)
				 
			    },
			    webSocketOnMessage(e){
			      console.log('来自服务端的消息->',e)
				  console.log(e.data)
			
				  // console.log(e.data)
				  // this.respond=JSON.parse(e.data)
				  // console.log(this.respond.message)
				  this.getmessage=JSON.parse(e.data)
				  console.log(this.getmessage.message)
			
			    },
			    webSocketOnError(e){
			      console.log('与服务端连接异常->',e)
			    },
			    webSocketOnClose(e){
			      console.log('与服务端连接关闭->',e)
			    },
				  handleSendButton() {
					// this.webSocketObject.send(JSON.stringify({
					//  messageType:1,
					//          userId:1,
					//    // chatId:2,
					//       // message:this.input,
					//      code:10001
					// }))
					//发送信息
				      this.webSocketObject.send(JSON.stringify({
				       messageType:1,
				               userId:2,
				         chatId:1,
				            message:this.input,
				           code:10002
				      }))
				 // this.inputform.message=this.input
				 // this.msglist.push(this.inputform)
				 // this.gengduoflag=false
				 //      this.input = ''   
				},
		}
	}
</script>

<style>

</style>
